package com.zhudeshuai.zhudemolist.widget.CustomerProcess;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

import com.zhudeshuai.zhudemolist.widget.GraphicalView;

/**
 * 自定义进度view
 *
 * @author zhudeshuai
 * @since 16/6/29
 */
public class CircleProcessView extends GraphicalView {


    private int mRadius = 0;
    /**
     * 当前进度
     */
    private int mProcess = 20;

    private int mPadding = 20;

    private Paint mBottomPaint;

    private Paint mProcessPaint;

    private Paint mTopPaint;

    private RectF mRectF;
    private Paint mTextPaint;

    private Paint.FontMetricsInt mFontMetricsInt;
    private int mTextY;
    private String mText;

    public CircleProcessView(Context context) {
        this(context, null);
    }

    public CircleProcessView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        mBottomPaint = new Paint();
        mBottomPaint.setColor(Color.parseColor("lightgrey"));
        mTopPaint = new Paint();
        mTopPaint.setColor(Color.WHITE);
        mProcessPaint = new Paint();
        mProcessPaint.setColor(Color.GREEN);
        mRectF = new RectF();


        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setStrokeWidth(3);
        mTextPaint.setTextSize(60);
        mTextPaint.setColor(Color.GREEN);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setStyle(Paint.Style.STROKE);
        mFontMetricsInt = mTextPaint.getFontMetricsInt();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mRadius = Math.min(getMeasuredWidth(), getMeasuredHeight()) / 2 - mPadding;
        mTextY = (getMeasuredHeight() - mFontMetricsInt.ascent - mFontMetricsInt.descent) / 2;
        mText = mProcess + "%";
    }

    @Override
    public void render(Canvas canvas) {
        mRectF.set(mPadding, mPadding, mPadding + 2 * mRadius, mPadding + 2 * mRadius);
        canvas.drawColor(Color.WHITE);
        canvas.drawCircle(mPadding + mRadius, mPadding + mRadius, mRadius, mBottomPaint);

        canvas.drawArc(mRectF, -90, mProcess * 360 / 100, true, mProcessPaint);

        canvas.drawCircle(mPadding + mRadius, mPadding + mRadius, mRadius * 2 / 3, mTopPaint);

        //绘制中间文字
        canvas.drawText(mProcess + "%", mPadding + mRadius - mText.length() / 2, mTextY, mTextPaint);
    }

    public void setProcess(int process){
        this.mProcess = process;
        refreshChart();
    }

}
